package com.explorati.interview.interview.Thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintABCUsingLock {

	private int times;
	private int state;
	private Lock lock = new ReentrantLock();

	public PrintABCUsingLock(int times) {
		this.times = times;
	}

	public static void main(String[] args) {
		PrintABCUsingLock printABCUsingLock = new PrintABCUsingLock(10);
		new Thread(() -> {
			printABCUsingLock.printA();
		}).start();
		new Thread(() -> {
			printABCUsingLock.printB();
		}).start();
		new Thread(() -> {
			printABCUsingLock.printC();
		}).start();
	}

	public void printA() {
		print("A", 0);
	}

	public void printB() {
		print("B", 1);
	}

	public void printC() {
		print("C", 2);
	}

	private void print(String name, int targetState) {
		for (int i = 0; i < times;) {
			lock.lock();
			if (state % 3 == targetState) {
				state++;
				i++;
				System.out.print(name);
			}
			lock.unlock();
		}
	}
}
